package com.geekjam.dingdongandroid;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import com.geekjam.dingdongandroid.api.DingDongMessageProxy;

import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * User: shou1dwe
 * Date: 10/3/13
 * Time: 11:11 AM
 * To change this template use File | Settings | File Templates.
 */
public class ScheduledAlarmActivity extends Activity {
    private MediaPlayer mPlayer;
    private String LOG_TAG = "DingDong - MyAlarmService";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.scheduled_alarm_screen);

        Button buttonDismiss = (Button) findViewById(R.id.dismiss_button);
        buttonDismiss.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                mPlayer.release();
                mPlayer = null;
                finish();
            }
        });

        mPlayer = new MediaPlayer();
        try {
            mPlayer.setDataSource(DingDongMessageProxy.RECEIVE_FILENAME);
            mPlayer.prepare();
            mPlayer.start();
            mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    mPlayer.start();
                }
            });
        } catch (IOException e) {
            Log.e(LOG_TAG, "prepare() failed");
        }
    }

    private void stopPlaying() {
        mPlayer.release();
        mPlayer = null;
    }
}
